<template>
  <div>
    <br />
    <div style="margin-left: 10px;">
      <i class="el-icon-paperclip" />
      <span v-if="userId===null"> 所有会员卡列表</span>
      <span v-else> {{userName}}的会员卡列表</span>
    </div>
    <el-card style="width: 98%; margin-left: 10px;">
      <br />
      <el-row>
        <el-col :span="16">
          <el-dropdown @command="handleCommand" placement="bottom" style="margin-right: 10px;margin-left: 30px;">
            <el-button type="primary" size="medium">
              <i class="el-icon-user-solid"></i> 会员选择<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown" style="max-height: 210px;overflow: overlay;width: 120px;">
              <el-dropdown-item command="all">所有会员</el-dropdown-item>
              <el-dropdown-item v-for="user in userList" v-text="user.userName" :command="composeValue(user)" />
            </el-dropdown-menu>
          </el-dropdown>
          <el-button type="primary" icon="el-icon-bank-card" size="medium" @click="addCardBtn">会员卡注册</el-button>
          <el-button type="primary" icon="el-icon-coin" size="medium" @click="rechargeBtn">充值记录查询</el-button>
          <el-button type="success" icon="el-icon-shopping-cart-full" size="medium" @click="consumeBtn">消费记录查询</el-button>
        </el-col>
        <el-col :span="6">
          <el-input placeholder="请输入要查询的会员卡ID" prefix-icon="el-icon-search" style="margin-left:30px;"
            :disabled="cardList.length===0&&query.length===0" clearable v-model="query"
            @clear="getCardListByLike" @input="getCardListByLike"></el-input>
        </el-col>
      </el-row>
      <br />
      <el-table :data="cardList" height="580" border
      v-loading="loading"
      :element-loading-text="tips"
      element-loading-spinner="el-icon-error"
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}" style="width: 97%;margin-left: 20px;">
        <el-table-column prop="id" label="会员卡号" width="330px"></el-table-column>
        <el-table-column prop="money" label="会员卡余额" width="220px">
          <template slot-scope="scope">
            <span style="color: saddlebrown;">￥</span>
            <span v-text="scope.row.money" style="color: saddlebrown;"></span>
          </template>
        </el-table-column>
        <el-table-column prop="integral" label="积分" width="220px"></el-table-column>
        <el-table-column prop="status" label="状态" width="248px">
          <template slot-scope="scope">
            <el-tag type="success" style="font-size: 15px;" v-if="scope.row.status===1">正常</el-tag>
            <el-tag type="danger" style="font-size: 15px;" v-else>挂失</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="500px">
          <template slot-scope="scope">
            <div v-if="scope.row.status===1">
              <el-button type="primary" size="medium" @click="updateMoneyBtn(scope.row)">充值</el-button>
              <el-button type="primary" size="medium" @click="updateCardBtn(scope.row)">消费</el-button>
              <el-button type="warning" size="medium" @click="updateStatus(scope.row)">挂失</el-button>
              <el-button type="danger" size="medium" @click="deleteCard(scope.row)">注销</el-button>
            </div>
            <div v-else-if="scope.row.status===0">
              <el-button type="primary" size="medium" disabled>充值</el-button>
              <el-button type="primary" size="medium" disabled>消费</el-button>
              <el-button type="warning" size="medium" @click="updateStatus(scope.row)">解挂</el-button>
              <el-button type="warning" size="medium" @click="makeupCard(scope.row)">补卡</el-button>
              <el-button type="danger" size="medium" @click="deleteCard(scope.row)">注销</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <br />
    </el-card>
    <!-- 会员卡注册对话框 -->
    <el-dialog title="会员卡注册" :visible.sync="addCardDialog" width="40%" @close="closeAddCardDialog">
      <el-form :model="addCardForm" :rules="cardRules" ref="addCardFormRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员姓名" prop="userId">
              <el-select v-model="addCardForm.userId" filterable v-if="userId===null">
                <el-option v-for="user in userList" :key="user.id" :label="user.userName" :value="user.id"></el-option>
              </el-select>
              <el-select v-model="addCardForm.userName" filterable disabled v-else>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="充值金额" prop="money" style="width: 90%;">
              <el-input v-model="addCardForm.money" @input="getReturn"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form :model="addRechargeForm" :rules="addRechargeRules" ref="addRechargeFormRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="支付方式" prop="payment">
              <el-select v-model="addRechargeForm.payment">
                <el-option label="电子支付" value="1"></el-option>
                <el-option label="现金支付" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="addRechargeForm.payment==1">
            <el-form-item label="支付渠道" prop="mode">
              <el-select v-model="addRechargeForm.mode">
                <el-option value="微信"></el-option>
                <el-option value="支付宝"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="addRechargeForm.payment==0">
            <el-form-item label="实际支付" prop="realMoney" style="width: 90%;">
              <el-input v-model="addRechargeForm.realMoney" @input="getReturn"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="addRechargeForm.realMoney!==''
                &&addRechargeForm.realMoney!==null
                &&addCardForm.money!==''
                &&addCardForm.money!==null">
          <el-col :span="12">
            <el-form-item label="零钱找回" prop="returnMoney" style="width: 90%;">
              <el-input v-model="addRechargeForm.returnMoney"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCardDialog = false">取 消</el-button>
        <el-button type="primary" @click="addCard">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 充值记录查询对话框 -->
    <el-dialog title="充值记录查询" :visible.sync="rechargeDialog" width="50%" @close="closeRechargeDialog">
      <el-table :data="rechargeList" max-height="350px"
      ref="filterTable" :default-sort = "{prop: 'dealTime', order: 'descending'}"
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}"
      style="width: 97%;margin-bottom: 30px;margin-left: 8px;">
        <el-table-column prop="id" label="交易单号"></el-table-column>
        <el-table-column prop="cardId" label="会员卡号"
        :filter-method="selectCardId"  filter-placement="bottom"
        :filters="selectList"></el-table-column>
        <el-table-column prop="moneyChange" label="充值金额" sortable>
          <template slot-scope="scope">
            <span style="color: saddlebrown;">￥</span>
            <span v-text="scope.row.moneyChange" style="color: saddlebrown;"></span>
          </template>
        </el-table-column>
        <el-table-column prop="dealTime" label="充值时间" width="260px" sortable></el-table-column>
      </el-table>
      <span>
        <el-button type="primary" @click="rechargeDialog = false" style="margin-left: 420px;">关 闭</el-button>
      </span>
    </el-dialog>
    <!-- 消费记录查询对话框 -->
    <el-dialog title="消费记录查询" :visible.sync="consumeDialog" width="52%" @close="closeConsumeDialog">
      <el-table :data="consumeList" max-height="350px"
      ref="filterTable" :default-sort = "{prop: 'dealTime', order: 'descending'}"
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}"
      style="width: 98%;margin-bottom: 30px;">
        <el-table-column prop="id" label="交易单号" width="150px"></el-table-column>
        <el-table-column prop="cardId" label="会员卡号" width="150px"
        :filter-method="selectCardId"  filter-placement="bottom"
        :filters="selectList"></el-table-column>
        <el-table-column prop="movieName" label="消费电影" width="180px"></el-table-column>
        <el-table-column prop="num" label="数量" width="80px"></el-table-column>
        <el-table-column prop="moneyChange" label="交易金额" width="120px" sortable>
          <template slot-scope="scope">
            <span style="color: saddlebrown;">￥</span>
            <span v-text="scope.row.moneyChange" style="color: saddlebrown;"></span>
          </template>
        </el-table-column>
        <el-table-column prop="dealTime" label="充值时间" width="210px" sortable></el-table-column>
      </el-table>
      <span>
        <el-button type="primary" @click="consumeDialog = false" style="margin-left: 440px;">关 闭</el-button>
      </span>
    </el-dialog>
    <!-- 会员卡充值对话框 -->
    <el-dialog title="会员卡充值" :visible.sync="updateMoneyDialog" width="40%" @close="closeUpdateMoneyDialog">
      <el-form :model="updateCardForm" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员卡号" prop="id" style="width: 90%;">
              <el-input v-model="updateCardForm.id" readonly @focus="warning"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员姓名" prop="userName" style="width: 90%;">
              <el-input v-model="updateCardForm.userName" readonly @focus="warning"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员卡余额" prop="money" style="width: 90%;">
              <el-input v-model="updateCardForm.money" readonly @focus="warning"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form :model="rechargeForm" :rules="rechargeRules" ref="rechargeFormRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="充值金额" prop="price" style="width: 90%;">
              <el-input v-model="rechargeForm.price" @input="getReturnMoney"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付方式" prop="payment">
              <el-select v-model="rechargeForm.payment">
                <el-option label="电子支付" value="1"></el-option>
                <el-option label="现金支付" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="rechargeForm.payment==1">
            <el-form-item label="支付渠道" prop="mode">
              <el-select v-model="rechargeForm.mode">
                <el-option value="微信"></el-option>
                <el-option value="支付宝"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="rechargeForm.payment==0">
          <el-col :span="12">
            <el-form-item label="实际支付" prop="realMoney" style="width: 90%;">
              <el-input v-model="rechargeForm.realMoney" @input="getReturnMoney"/>
            </el-form-item>
          </el-col>
          <el-col :span="12"
                  v-if="rechargeForm.realMoney!==''
                  &&rechargeForm.realMoney!==null
                  &&rechargeForm.price!==''
                  &&rechargeForm.price!==null">
            <el-form-item label="零钱找回" prop="returnMoney" style="width: 90%;">
              <el-input v-model="rechargeForm.returnMoney" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateMoneyDialog = false">取 消</el-button>
        <el-button type="primary" @click="updateMoney">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 会员卡消费对话框 -->
    <el-dialog title="会员卡消费" :visible.sync="updateCardDialog" width="40%" @close="closeUpdateCardDialog">
      <el-form :model="updateCardForm" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员姓名" prop="userName" style="width: 90%;">
              <el-input v-model="updateCardForm.userName" readonly @focus="warning"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员卡号" prop="id" style="width: 90%;">
              <el-input v-model="updateCardForm.id" readonly @focus="warning"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员卡余额" prop="money" style="width: 90%;">
              <el-input v-model="updateCardForm.money" readonly @focus="warning"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员卡积分" prop="integral" style="width: 90%;">
              <el-input v-model="updateCardForm.integral" readonly @focus="warning"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form :model="movieForm" :rules="movieRules" ref="movieFormRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="订购电影" prop="id">
              <el-select v-model="movieForm.id" filterable placeholder="请选择" @change="getMovie">
                <el-option v-for="movie in movieList" :key="movie.id" :label="movie.name" :value="movie.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订购票数" prop="num">
              <el-input-number v-model="movieForm.num" @change="handleChange" :min="1">
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电影价格" prop="price" style="width: 90%;">
              <el-input v-model="movieForm.price" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电影积分" prop="integral" style="width: 90%;">
              <el-input v-model="movieForm.integral" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付金额" prop="moneySum" style="width: 90%">
              <el-input v-model="movieForm.moneySum" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="获取积分" prop="integralSum" style="width: 90%">
              <el-input v-model="movieForm.integralSum" readonly></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateCardDialog = false">取 消</el-button>
        <el-button type="primary" @click="updateCard">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    created() {
      this.getCardList()
      this.getUserList()
      this.getMovieList()
      this.getSelectList()
      this.getRechargeList()
      this.getConsumeList()
    },
    data() {
      //校验手机号的邮箱规则
      const checkPhone = (rule, value, callback) => {

        //定义校验手机号的正则语法
        const phoneRege = /^1[34578][0-9]{9}$/
        if (phoneRege.test(value)) {

          return callback()
        }
        callback(new Error('请填写正确的手机号'))

      }

      const checkPassword = (rule, value, callback) => {
        if (this.addUserForm.password !== value) return callback(new Error('2次密码输入不一致'))
        //否则校验成功
        callback()
      }
      const checkPrice = (rule, value, callback) => {
        //定义充值金额的正则表达式  JS中用/来表示正则表达式的开始和结束
        const priceRege =
          /(^[1-9]{1}[0-9]*$)|(^0[\.]{1}0[1-9]{1}$)|(^0[\.]{1}[1-9]{1}[0-9]{0,1}$)|(^[1-9]{1}[0-9]*[\.]{1}[0-9]{1,2}$)/
        // const emailRege = /^[a-zA-Z0-9-_]+@[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+$/
        //正则表达式语法校验  test(xx) 校验成功 返回true  校验失败返回false
        if (priceRege.test(value)) {
          //表示金额合法 正确返回
          return callback()
        }
        callback(new Error('请填写正确的充值金额'))
      }
      return {
        query: '',
        recharQuery:'',
        consumeQuery:'',
        tips: '',
        loading: '',
        userId: null,
        userName: null,
        selectList:[],
        userList: [],
        cardList: [],
        movieList: [],
        movieForm: {
          id: null,
          num: 1
        },
        rechargeForm: {
          price: null,
          realMoney: null,
          returnMoney: null
        },
        addRechargeForm: {
          price: null,
          realMoney: null,
          returnMoney: null
        },
        addCardForm: {
          money:null
        },
        updateCardForm: {},
        rechargeDialog:false,
        consumeDialog:false,
        addCardDialog: false,
        updateMoneyDialog: false,
        updateCardDialog: false,
        cardRules: {
          userId: [{
            required: true,
            message: '请选择会员',
            trigger: 'blur'
          }],
          money: [{
              required: true,
              message: '请输入要充值的余额',
              trigger: 'blur'
            },
            {
              validator: checkPrice,
              trigger: 'blur'
            }
          ]
        },
        userRules: {
          userName: [{
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 30,
              message: '长度在 3 到 30 个字符',
              trigger: 'blur'
            }
          ],
          password: [{
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 30,
              message: '长度在 3 到 30 个字符',
              trigger: 'blur'
            }
          ],
          password2: [{
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 30,
              message: '长度在 3 到 30 个字符',
              trigger: 'blur'
            },
            {
              validator: checkPassword,
              trigger: 'blur'
            }
          ],
          phone: [{
              required: true,
              message: '请输入手机号',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 30,
              message: '长度在 3 到 30 个字符',
              trigger: 'blur'
            },
            {
              validator: checkPhone,
              trigger: 'blur'
            }
          ],
          sex: [{
            required: true,
            message: '请选择性别',
            trigger: 'blur'
          }],
          birthday: [{
            required: true,
            message: '请选择日期',
            trigger: 'blur'
          }]
        },
        addRechargeRules: {
          payment: [{
            required: true,
            message: '请选择支付方式',
            trigger: 'blur'
          }],
          mode: [{
            required: true,
            message: '请选择支付渠道',
            trigger: 'blur'
          }],
          realMoney: [{
              required: true,
              message: '请输入实际支付的金额',
              trigger: 'blur'
            },
            {
              validator: checkPrice,
              trigger: 'blur'
            }
          ],
          returnMoney: [{
            required: true,
            trigger: 'blur'
          }]
        },
        rechargeRules: {
          price: [{
              required: true,
              message: '请输入要充值的余额',
              trigger: 'blur'
            },
            {
              validator: checkPrice,
              trigger: 'blur'
            }
          ],
          payment: [{
            required: true,
            message: '请选择支付方式',
            trigger: 'blur'
          }],
          mode: [{
            required: true,
            message: '请选择支付渠道',
            trigger: 'blur'
          }],
          realMoney: [{
              required: true,
              message: '请输入实际支付的金额',
              trigger: 'blur'
            },
            {
              validator: checkPrice,
              trigger: 'blur'
            }
          ],
          returnMoney: [{
            required: true,
            trigger: 'blur'
          }]
        },
        movieRules: {
          id: [{
            required: true,
            message: '请选择要订购的电影',
            trigger: 'blur'
          }],
          num: [{
            required: true,
            message: '请选择订购票数',
            trigger: 'blur'
          }],
          price: [{
            required: true,
            message: '电影价格不能为空',
            trigger: 'blur'
          }],
          integral: [{
            required: true,
            message: '电影积分不能为空',
            trigger: 'blur'
          }],
          moneySum: [{
            required: true,
            message: '支付金额不能为空',
            trigger: 'blur'
          }],
          integralSum: [{
            required: true,
            message: '获取积分不能为空',
            trigger: 'blur'
          }],
        }
      }
    },
    methods: {
      async getUserList() {
        const {
          data: result
        } = await this.$http.get('user/getUserName')
        this.userList = result.data
        for (let user of this.userList) {
          const {
            data: result
          } = await this.$http.get(`card/getCardList?userId=${user.id}`)
          if (result.data.length === 0) {
            user.flag = true
          }
        }
      },
      async getMovieList() {
        const {
          data: result
        } = await this.$http.get('/movie/getMovieList')
        this.movieList = result.data
      },
      async getCardList() {
        const {
          data: result
        } = await this.$http.get('card/list?query=' + this.query)
        if (result.status !== 200) return this.$message.error("会员卡列表显示失败!")
        this.cardList = result.data
        if (this.cardList.length === 0 && this.query.length === 0) {
          this.loading = true
          this.tips = '系统未注册任何会员卡'
        } else {
          this.loading = false
          this.userId = null
          this.userName = null
          this.tips = '当前会员未注册任何会员卡'
        }
      },
      init() {
        if (this.userId !== null) {
          this.getCardListByUserId()
        } else {
          this.getCardList()
        }
      },
      getCardListByLike(){
        if(this.userId===null||this.userId===''){
          this.getCardList()
        }else{
          this.getCardListByUserId()
        }
      },
      async getCardListByUserId() {
        const {
          data: result
        } = await this.$http.get(`card/getCardList?userId=${this.userId}&query=${this.query}`)
        this.cardList = result.data
      },
      async handleCommand(command) {
        this.query = ''
        this.loading = false
        if (command === "all") {
          this.getCardList()
          if (this.cardList.length === 0 && this.userId === null) {
            this.$message.error("系统未注册任何会员卡!")
          } else {
            this.$message.success("所有会员卡列表展示成功!")
          }
          return
        }
        this.userId = command.userId
        this.userName = command.userName
        if (command.flag === true) {
          this.$notify.warning({
            title: '注意',
            dangerouslyUseHTMLString: true,
            message: '<div>注册会员卡后可享受更多优惠</div><div>需要注册个会员卡吗?</div>',
            position: 'top-left'
          })
          this.cardList = []
          this.loading = true
        } else {
          this.getCardListByUserId()
          this.$message.success(command.userName + "的会员卡列表展示成功!")
        }
      },
      composeValue(user) {
        return {
          'userId': user.id,
          'userName': user.userName,
          'flag': user.flag
        }
      },
      closeAddCardDialog() {
        this.$refs.addCardFormRef.resetFields()
        this.$refs.addRechargeFormRef.resetFields()
        this.addCardForm = {
          money:null
        }
        this.addRechargeForm = {
          price: null,
          realMoney: null,
          returnMoney: null
        }
      },
      getReturn() {
        this.addRechargeForm.returnMoney = this.addRechargeForm.realMoney - this.addCardForm.money
        if (this.addRechargeForm.returnMoney < 0) this.addRechargeForm.returnMoney = null
      },
      addCardBtn() {
        this.addCardDialog = true
        if (this.userId !== null) {
          this.addCardForm.userId = this.userId
          this.addCardForm.userName = this.userName
        }
      },
      async addCard() {
        if (this.addRechargeForm.payment == 0) {
          if(this.addRechargeForm.realMoney!==''
            &&this.addRechargeForm.realMoney!==null
            &&this.addCardForm.money!==''
            &&this.addCardForm.money!==null){
            if (this.addRechargeForm.returnMoney === null) {
              this.$message.error("当前金额不足以完成支付操作!")
              return
            }
          }
        }
        this.$refs.addCardFormRef.validate(async valid1 => {
          //如果校验失败 则停止数据
          this.$refs.addRechargeFormRef.validate(async valid2 => {
            if (!valid1||!valid2) return
            this.addRechargeForm.price=this.addCardForm.money
            let form={
              card:this.addCardForm,
              recharge:this.addRechargeForm
            }
            const {
              data: result
            } = await this.$http.post(`card/add`, form)
            if (result.status !== 200) return this.$message.error("会员卡注册失败!")
            this.$message.success("会员卡注册成功!")
            this.addCardDialog = false
            this.loading = false
            if (this.cardList.length === 0) {
              this.query = ''
            }
            this.init()
            this.getUserList()
            this.getRechargeList()
          })
        })
      },
      closeRechargeDialog(){
        this.$refs.filterTable.clearFilter();
      },
      rechargeBtn(){
        this.rechargeDialog=true
      },
      async getRechargeList(){
        const {data: result} =await this.$http.get(`deal/getRechargeList`)
        this.rechargeList=result.data
      },
      selectCardId(value,row){
        return row.cardId === value
      },
      async getSelectList(){
        const {data: result} =await this.$http.get(`deal/getSelectList`)
        this.selectList=result.data
      },
      closeConsumeDialog(){
        this.$refs.filterTable.clearFilter();
      },
      consumeBtn(){
        this.consumeDialog=true
      },
      async getConsumeList(){
        const {data: result} =await this.$http.get(`deal/getConsumeList`)
        this.consumeList=result.data
      },
      warning() {
        this.$notify.warning({
          title: '注意',
          dangerouslyUseHTMLString: true,
          message: '<div>会员卡充值或消费过程中</div><div>不可以修改会员卡的信息哦~</div>',
          offset: 100
        });
      },
      closeUpdateMoneyDialog() {
        this.$refs.rechargeFormRef.resetFields()
        this.rechargeForm = {
          price: null,
          realMoney: null,
          returnMoney: null
        }
      },
      getReturnMoney() {
        this.rechargeForm.returnMoney = this.rechargeForm.realMoney - this.rechargeForm.price
        if (this.rechargeForm.returnMoney < 0) this.rechargeForm.returnMoney = null
      },
      async updateMoneyBtn(card) {
        this.init()
        this.updateCardForm = card
        if (this.userId === null) {
          for (let user of this.userList) {
            if (card.userId === user.id) {
              this.updateCardForm.userName = user.userName
              break
            }
          }
        } else {
          this.updateCardForm.userName = this.userName
        }
        this.updateMoneyDialog = true
      },
      async updateMoney() {
        if (this.rechargeForm.payment == 0) {
          if(this.rechargeForm.realMoney!==''
            &&this.rechargeForm.realMoney!==null
            &&this.rechargeForm.price!==''
            &&this.rechargeForm.price!==null){
            if (this.rechargeForm.returnMoney === null) {
              this.$message.error("当前金额不足以完成支付操作!")
              return
            }
          }
        }
        this.$refs.rechargeFormRef.validate(async valid => {
          //如果校验失败 则停止数据
          if (!valid) return
          this.updateCardForm.userName = ''
          this.updateCardForm.price = this.rechargeForm.price
          this.rechargeForm.cardId = this.updateCardForm.id
          let form = {
            card: this.updateCardForm,
            recharge: this.rechargeForm
          }
          const {
            data: result
          } = await this.$http.put('card/updateMoney', form)
          if (result.status !== 200) return this.$message.error("会员卡充值失败!")
          this.$message.success("会员卡充值成功!")
          this.init()
          this.getRechargeList()
          this.updateMoneyDialog = false
        })
      },
      closeUpdateCardDialog() {
        this.$refs.movieFormRef.resetFields()
        this.movieForm = {
          id: null,
          num: 1
        }
      },
      async getMovie(id) {
        let num = this.movieForm.num
        const {
          data: result
        } = await this.$http.get('/movie/getMovie?id=' + id)
        this.movieForm = result.data
        this.movieForm.moneySum = this.movieForm.price * num
        this.movieForm.integralSum = this.movieForm.integral * num
        this.movieForm.num = num
        this.$refs.movieFormRef.resetFields()
      },
      handleChange(num) {
        if (this.movieForm.id !== null) {
          this.movieForm.moneySum = this.movieForm.price * num
          this.movieForm.integralSum = this.movieForm.integral * num
        }
      },
      async updateCardBtn(card) {
        this.init()
        this.updateCardForm = card
        if (this.userId === null) {
          for (let user of this.userList) {
            if (card.userId === user.id) {
              this.updateCardForm.userName = user.userName
              break
            }
          }
        } else {
          this.updateCardForm.userName = this.userName
        }
        this.updateCardDialog = true
      },
      async updateCard() {
        this.$refs.movieFormRef.validate(async valid => {
          //如果校验失败 则停止数据
          if (!valid) return
          if (this.updateCardForm.money < this.movieForm.price * this.movieForm.num) {
            this.$message.error("会员卡余额不足，无法进行消费!")
            return
          }
          let form = {
            card: this.updateCardForm,
            movie: this.movieForm
          }
          const {
            data: result
          } = await this.$http.put('card/updateCard', form)
          if (result.status !== 200) return this.$message.error("会员卡消费失败!")
          this.$message.success("会员卡消费成功!")
          this.init()
          this.getConsumeList()
          this.updateCardDialog = false
        })
      },
      async updateStatus(card) {
        if (card.status === 1) {
          this.$confirm('确定要挂失此会员卡吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(async () => {
            //传递分类id
            const {
              data: result
            } = await this.$http.put("/card/updateStatus", card)
            if (result.status !== 200) return this.$message.error("会员卡挂失失败")
            this.$message.success("会员卡挂失成功")
            this.init()
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消挂失'
            });
          });
        } else {
          this.$confirm('确定要解挂此会员卡吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(async () => {
            //传递分类id
            const {
              data: result
            } = await this.$http.put("/card/updateStatus", card)
            if (result.status !== 200) return this.$message.error("会员卡解挂失败")
            this.$message.success("会员卡解挂成功")
            this.init()
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消解挂'
            });
          });
        }
      },
      async makeupCard(card) {
        this.$confirm('确定要补办此会员卡吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //传递分类id
          const {
            data: result
          } = await this.$http.put("/card/updateStatus", card)
          if (result.status !== 200) return this.$message.error("会员卡补办失败")
          this.$message.success("会员卡补办成功")
          this.init()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消补卡'
          });
        });
      },
      async deleteCard(card) {
        this.$confirm('确定要注销此会员卡吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //传递分类id
          const {
            data: result
          } = await this.$http.delete(`/card/deleteCard`, {
            params: {
              id: card.id,
              userId: card.userId
            }
          })
          if (result.status !== 200) return this.$message.error("会员卡注销失败")
          this.$message.success("会员卡注销成功")
          if (result.data === true) {
            for (let user of this.userList) {
              if (card.userId === user.id) {
                user.flag = true
                this.loading = true
              }
            }
          }
          this.init()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消注销'
          });
        });
      }
    }
  }
</script>

<style>

</style>
